package com.xm.dao;

import java.sql.ResultSet;
import java.sql.SQLException;

import com.xm.entity.Users;
import com.xm.uti.DBManager;
import com.xm.uti.QueryDataResult;

public class UserDaoImpl {
/**
 * 获取用户
 * @param userName
 * @param userPwd
 * @return
 */
	public Users getUsers(String userName,String userPwd) {
		String sql = "select * from users where userName='"+userName+"' and userPwd='"+userPwd+"' ";
		System.out.println(sql);
		QueryDataResult executeQuery = DBManager.executeQuery(sql);
		if(executeQuery != null) {
			ResultSet rs = executeQuery.rs;
			try {
				while(rs.next()) {
					Users user = new Users();
					user.setUserName(rs.getString("userName"));
					user.setUserPwd(rs.getString("userPwd"));
					user.setUserUrl(rs.getString("userUrl"));
					return user;
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}finally {
				executeQuery.close();
			}
		}
		return null;
	}
	/**
	 * 修改方法
	 * @param userName
	 * @param pwd
	 * @return
	 */
	public boolean updateUsers(String userName,String pwd) {
		String sql = "update users set userPwd='"+pwd+"' where userName='"+userName+"'";
		System.out.println(sql);
		int i = DBManager.executeUpdate(sql);
		if(i>0) {
			return true;
		}
		return false;
	}
	
	/**
	 * 修改头像
	 * @param userName
	 * @param pwd
	 * @return
	 */
	public boolean updatePhose(String id,String userUrl) {
		String sql = "update users set userUrl='"+userUrl+"' where id='"+id+"'";
		System.out.println(sql);
		int i = DBManager.executeUpdate(sql);
		if(i>0) {
			return true;
		}
		return false;
	}
	
	/**
	 * 用户
	 * @param 
	 * @return
	 */
	public Users Users(String userName) {
		String sql = "select * from users where userName = '"+userName+"'";
		System.out.println(sql);
		QueryDataResult executeQuery = DBManager.executeQuery(sql);
		if(executeQuery != null) {
			ResultSet rs = executeQuery.rs;
			try {
				while(rs.next()) {
					Users user = new Users();
					user.setUserName(rs.getString("userName"));
					user.setUserPwd(rs.getString("userPwd"));
					user.setUserUrl(rs.getString("userUrl"));
					return user;
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}finally {
				executeQuery.close();
			}
		}
		return null;
	}

}